var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userVo: '', //个人信息
    bankInfo: '', //银行卡信息
    amount: '', //总金额
    bankStatus: '', //绑定银行卡状态
    isaddcss: true
    // isaddcss: false
  },
  onShow: function() {
    this.getAccountInfo();
    this.onMineInfo();
    this.onBankInfo();
  },
  //跳转明细页面
  goBalanceDetail: function() {
    wx.navigateTo({
      url: 'balanceDetail'
    });
  },
  //跳转充值页面
  goCz: function() {
    var that = this;
    that.setData({
      isaddcss: true
    });
    wx.navigateTo({
      url: 'paymentCard'
    });
  },
  goGetCash: function() {
    var that = this;
    that.setData({
      isaddcss: false
    });
    if (that.data.userVo.auditStatus != 3) {
      //认证状态
      wx.showModal({
        // title: '提示',
        content: '为了保障平台账户资金安全，请完成行业认证后使用该功能',
        cancelText: '取消',
        confirmText: '去认证',
        confirmColor: '#50BEC5',
        success: function(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: 'authentication'
            });
          } else if (res.cancel) {
            // console.log('用户点击取消')
          }
        }
      });
    } else {
      if (that.data.bankStatus == 1) {
        wx.showModal({
          title: '提示',
          content:
            '使用提现功能前，需要完成银行卡认证，校验该银行卡为提现本人持有',
          confirmText: '去认证',
          confirmColor: '#56BDE0',
          success(res) {
            if (res.confirm) {
              wx.navigateTo({ url: 'bindBandCard?payStatus=' });
            } else if (res.cancel) {
              //console.log('用户点击取消')
            }
          }
        });
      } else {
        wx.navigateTo({
          url: 'getCash?amount=' + that.data.amount
        });
      }
    }
  },

  onMineInfo: function() {
    var that = this;
    wx.request({
      method: 'POST',
      url: app.preUrl + 'findByUserId',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: app.globalData.userId
      },
      success: function(res) {
        console.log(res.data.result);
        if (res.data.code == 100) {
          that.setData({
            userVo: res.data.result
          });
        }
      },
      fail: function(error) {
        console.log(error);
      }
    });
  },
  //查询银行卡信息
  onBankInfo: function() {
    var that = this;
    wx.request({
      method: 'POST',
      url: app.preUrl + 'bankUserId',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: app.globalData.userId
      },
      success: function(res) {
        console.log(res);
        if (res.data.code == 100) {
          console.log(res.data.result);
          that.setData({
            bankInfo: res.data.result,
            bankStatus: 2
          });
        } else if (res.data.code == 400) {
          console.log(res);
          that.setData({
            bankStatus: 1
          });
        }
      },
      fail: function(error) {
        console.log(error);
      }
    });
  },

  getAccountInfo() {
    wx.showLoading({ title: '加载中' });
    var that = this;
    wx.request({
      method: 'POST',
      url: app.preUrl + 'accountInfo',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: app.globalData.userId
      },
      success: function(res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.code == 100) {
          that.setData({ amount: res.data.result.amount });
        }
      },
      fail: function(error) {
        console.log(error);
      }
    });
  },
  //goQuestion
  goQuestion: function() {
    wx.navigateTo({
      url: 'question'
    });
  }
});
